using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Master : BaseMaster
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Page.ClientScript.RegisterStartupScript(this.GetType(), "MenuStartup", string.Format("startList(\"{0}\");", menuIssues.ClientID), true);
    }

    protected void btnSearch_Click(object sender, ImageClickEventArgs e)
    {
        if (Page.IsValid)
        {
            Response.Redirect(string.Format("~/Search.aspx?Text={0}", Page.Server.UrlEncode(txtSearch.Text)), true);
        }
    }

    protected void dsTopics_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.Topic> e)
    {
        int siteId = 1;
        e.SetData(Acms.Bll.Topic.GetAllBySiteId(siteId));
    }

    public override void SetTopicTitle(string topicName)
    {
        lblTopicName.Text = topicName;
    }

    public override ImageButton SearchButton
    {
        get { return btnSearch; }
    }

    public override TextBox SearchTextBox
    {
        get { return txtSearch; }
    }

    public override void SetTopic(Acms.Bll.Topic topic)
    {
        Image topicImageBackUp = upTopicImage.FindControl("topicImageBackUp") as Image;
        Acms.AcmsControls.AcmsImage topicImage = upTopicImage.FindControl("topicImage") as Acms.AcmsControls.AcmsImage;
        if (topicImageBackUp != null && topicImage != null)
        {
            Acms.Bll.TopicFile file = topic.GetAttachment("HeaderImage");
            if (file != null)
            {
                topicImageBackUp.Visible = false;
                topicImage.Visible = true;
                topicImage.FileId = file.FileId;
                topicImage.ToolTip = topic.Name;
            }
            else
            {
                topicImageBackUp.Visible = true;
                topicImage.Visible = false;
            }
        }
        upTopicImage.Update();
    }

    protected void rptTopics_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Acms.Bll.Topic topic = e.Item.DataItem as Acms.Bll.Topic;
            HyperLink link = e.Item.FindControl("lnkTopic") as HyperLink;
            Acms.AcmsControls.AcmsImage topicImage = upTopicImage.FindControl("topicImage") as Acms.AcmsControls.AcmsImage;
            Image topicImageBackUp = upTopicImage.FindControl("topicImageBackUp") as Image;

            if (link != null)
            {
                link.NavigateUrl = string.Format("{1}#Topic_{0}", topic.Id, Page.Request.RawUrl);
                link.Text = topic.Name;
                link.Attributes.Add("onclick", string.Format("javascript:return topicBarClick(this, {0}, \"{1}\", \"{2}\")", topic.Id.ToString(), topicImage.ClientID, topicImageBackUp.ClientID));
                Acms.Bll.TopicFile file = topic.GetAttachment("HeaderImage");
                if (file != null)
                {
                    Page.ClientScript.RegisterExpandoAttribute(link.ClientID, "TopicImage", Page.ResolveClientUrl(string.Format("~/File.aspx?Id={0}", file.FileId)));
                }
                else
                {
                    Page.ClientScript.RegisterExpandoAttribute(link.ClientID, "TopicImage", Page.ResolveClientUrl("~/img/Classic_cocktail.jpg"));
                }
            }
        }
    }
    protected void rptTopics_ItemCreated(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            HtmlGenericControl li = new HtmlGenericControl("li");
            HyperLink link = new HyperLink();
            link.ID = "lnkTopic";
            li.Controls.Add(link);
            e.Item.Controls.Add(li);
        }
    }

    public override void FixTopicImage(string path)
    {
        topicImageBackUp.ImageUrl = path;
        topicImageBackUp.Visible = true;
        topicImage.Visible = false;
    }
}
